package com.qf.flash.ui.business;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.qf.flash.R;
import com.qf.flash.constant.FlashCodes;
import com.qf.flash.event.MoneySetEvent;
import com.qf.flash.http.response.ShopOrderBean;
import com.qf.flash.mvp.contract.BusOrderOptionContract;
import com.qf.flash.mvp.contract.ShopOrderContract;
import com.qf.flash.mvp.presenter.BusOrderOptionPresenter;
import com.qf.flash.mvp.presenter.ShopOrderPresenter;
import com.qf.flash.ui.business.adapter.MyBillItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.exception.ApiException;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/3/14 on 20:42
 * 描述: 商家我的订单
 * 作者:戴旭东
 */
public class MyBillActivity extends BaseActivity implements ShopOrderContract.View, BusOrderOptionContract.View {

    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tabLayout)
    XTabLayout tabLayout;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;

    private RVAdapter<ShopOrderBean> mAdapter;
    private int page = 1;
    protected boolean isRefresh;
    private ShopOrderPresenter mShopOrderPresenter;
    private String status = "";
    private BusOrderOptionPresenter mBusOrderOptionPresenter;

    public static void start(Activity activity) {
        Intent intent = new Intent(activity, MyBillActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected void initInject() {
        mShopOrderPresenter = new ShopOrderPresenter();
        mBusOrderOptionPresenter = new BusOrderOptionPresenter();
        initPresenters(mShopOrderPresenter, mBusOrderOptionPresenter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_bill;
    }

    @Override
    protected void initViewAndData() {
        tvTopTitle.setText("我的订单");
        initRefresh();
        initRecyclerView();
        initTabLayout();
        mRefresh.autoRefresh();
    }

    private void initTabLayout() {
        tabLayout.addTab(tabLayout.newTab().setText("全部"));
        tabLayout.addTab(tabLayout.newTab().setText("待完成"));
        tabLayout.addTab(tabLayout.newTab().setText("已完成"));
        tabLayout.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                int position = tab.getPosition();
                if (position == 2) {
                    status = "2";
                } else if (position == 1) {
                    status = "1";
                } else {
                    status = "";
                }
                mRefresh.autoRefresh();
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }

    private void initRecyclerView() {
        mAdapter = new RVAdapter<>(new MyBillItem());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new RVAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(View view, int position) {
                switch (view.getId()) {
                    case R.id.cv_group:
                        ShopOrderBean data = mAdapter.getData(position);
                        OrderDetailActivity.start(MyBillActivity.this, data.getPlayId(), data.getId());
                        break;
                    case R.id.tv_action:
                        ShopOrderBean bean = mAdapter.getData(position);
                        shopAction(bean);
                        break;

                }
            }
        });

    }

    private void initRefresh() {
        mRefresh.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isRefresh = false;
                page++;
                requestData();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                requestData();
            }

        });
    }

    private void requestData() {
        mShopOrderPresenter.getShopOrderData(status, page);
    }


    private void shopAction(ShopOrderBean bean) {
        //订单状态，0待付定金，1待组队，2待开始(完成组队)，3待结束，4已完成,5支付失败，6交易关闭，7商家已拒单
        //String ORDER_STATE_DFDJ = "0";
        //    String ORDER_STATE_DZD = "1";
        //    String ORDER_STATE_WCZD = "2";
        //    String ORDER_STATE_DJS = "3";
        //    String ORDER_STATE_YWC = "4";
        //    String ORDER_STATE_ZISB = "5";
        //    String ORDER_STATE_GBJY = "6";
        //    String ORDER_STATE_SJJD = "7";
        String orderState = bean.getOrderState();
        String orderId = bean.getId();
        switch (orderState) {
            //待付定金
            case FlashCodes.ORDER_STATE_DFDJ:
                break;
            //待组队
            case FlashCodes.ORDER_STATE_DZD:
                mBusOrderOptionPresenter.shopOpCompleted(orderId);
                break;
            //待开始(完成组队)
            case FlashCodes.ORDER_STATE_WCZD:
                //免费的情况， 可以直接开始
                if (TextUtils.equals(bean.getIsFree(), "1")) {
                    mBusOrderOptionPresenter.shopOpStartGame(orderId);
                } else if (bean.getIsSetRest() != 1) {
                    //没有设置尾款， 去设置页面
                    SetFeeActivity.start(this, bean.getId());
                } else {
                    //开始游戏
                    mBusOrderOptionPresenter.shopOpStartGame(orderId);
                }
                break;
            //待结束
            case FlashCodes.ORDER_STATE_DJS:
                //结束游戏
                mBusOrderOptionPresenter.shopOpEndGame(orderId);

                break;
            //4已完成  去设置担当
            case FlashCodes.ORDER_STATE_YWC:
                OrderDetailActivity.start(this, bean.getPlayId(), bean.getId());
                break;
            case FlashCodes.ORDER_STATE_ZISB:
                break;
            case FlashCodes.ORDER_STATE_GBJY:
                break;
            case FlashCodes.ORDER_STATE_SJJD:
                break;
        }
    }


    @OnClick(R.id.tv_top_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void setShopOrder(List<ShopOrderBean> shopOrderBeans, int totalPage) {
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
        mRefresh.setEnableLoadMore(page < totalPage);
        if (isRefresh) {
            mAdapter.setDatas(shopOrderBeans);
        } else {
            mAdapter.addData(shopOrderBeans);
        }
    }

    @Override
    public void onError(ApiException e) {
        super.onError(e);
        ViewUtil.setRefreshComplete(mRefresh, isRefresh);
    }

    @Override
    public void receiveOrRejectOrderSuccess(int optionType) {

    }

    @Override
    public void shopOpCompletedSuccess() {
        FlashLib.showToast("操作成功");
        mRefresh.autoRefresh();
    }

    @Override
    public void shopOpStartGameSuccess() {
        FlashLib.showToast("操作成功");
        mRefresh.autoRefresh();
    }

    @Override
    public void shopOpEndGameSuccess() {
        FlashLib.showToast("操作成功");
        mRefresh.autoRefresh();
    }

    @Override
    public void shopOpMoneySuccess() {
        FlashLib.showToast("操作成功");
        mRefresh.autoRefresh();
    }

    @Override
    protected boolean useEvent() {
        return true;
    }

    @Subscribe
    public void onSetFeeEvent(MoneySetEvent event) {
        double money = event.getMoney();
        mBusOrderOptionPresenter.shopOpMoney(event.getOrderId(), money);
    }
}
